<?php

namespace app\helpers;

use app\helpers\Str;

class Code
{
    //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
    private $API_SEND_URL='http://XXX/msg/send/json'; //创蓝发送短信接口URL

    private $API_VARIABLE_URL = 'http://XXX/msg/variable/json';//创蓝变量短信接口URL

    private $API_BALANCE_QUERY_URL= 'http://XXX/msg/balance/json';//创蓝短信余额查询接口URL

    private $API_ACCOUNT= ''; // 创蓝API账号

    private $API_PASSWORD= '';// 创蓝API密码

    private $mobile = '';

    private $tag = '';
    //参数的配置 请登录zz.253.com 获取以上API信息 ↑↑↑↑↑↑↑

    public function __construct($mobile)
    {
        $in_china = Str::startsWith($mobile,'86');
        $base_url = $in_china?'https://smssh1.253.com':'https://intapi.253.com';
        $this->API_SEND_URL = $base_url.'/msg/send/json';
        $this->API_VARIABLE_URL = $base_url.'/msg/variable/json';
        $this->API_BALANCE_QUERY_URL = $base_url.'/msg/balance/json';
        $this->API_ACCOUNT = $in_china?'N7616732':'I5663735';
        $this->API_PASSWORD = $in_china?'rqif7X1eBg9db2':'Db1GTxpM2vac34';
        // $this->API_ACCOUNT = $in_china?'N0021327':'I6722704';
        // $this->API_PASSWORD = $in_china?'Q958i2YUfN94e9':'iZ5bAODTdH28bb';
        $this->mobile = $in_china?Str::substr($mobile,2):$mobile;
        $this->tag = $in_china?'【途猪】':'';
    }

    /**
     * 发送短信
     *
     * @param string $mobile 		手机号码
     * @param string $msg 			短信内容
     * @param string $needstatus 	是否需要状态报告
     */
    public function sendSMS($msg, $needstatus = 'true') {

        //创蓝接口参数
        $postArr = array (
            'account'  =>  $this->API_ACCOUNT,
            'password' => $this->API_PASSWORD,
            'msg' => urlencode($this->tag.$msg),
            'phone' => $this->mobile,
            'report' => $needstatus,
        );
        $result = $this->curlPost( $this->API_SEND_URL, $postArr);
        file_put_contents('sendmsg.txt', '--start--' . PHP_EOL . $this->mobile . ':' . $msg . PHP_EOL . $result . PHP_EOL, FILE_APPEND);
        return $result;
    }

    /**
     * 发送变量短信
     *
     * @param string $msg 			短信内容
     * @param string $params 	最多不能超过1000个参数组
     */
    public function sendVariableSMS( $msg, $params) {

        //创蓝接口参数
        $postArr = array (
            'account'  =>  $this->API_ACCOUNT,
            'password' => $this->API_PASSWORD,
            'msg' => $msg,
            'params' => $params,
            'report' => 'true'
        );

        $result = $this->curlPost( $this->API_VARIABLE_URL, $postArr);
        return $result;
    }

    /**
     * 查询额度
     *
     *  查询地址
     */
    public function queryBalance() {

        //查询参数
        $postArr = array (
            'account'  =>  $this->API_ACCOUNT,
            'password' => $this->API_PASSWORD,
        );
        $result = $this->curlPost($this->API_BALANCE_QUERY_URL, $postArr);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     *
     */
    private function curlPost($url,$postFields){
        $postFields = json_encode($postFields);
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );
        return $result;
    }
}